<script setup lang="ts">
  import { getCurrentInstance, reactive, ref } from 'vue'
  import { reqAddNotice, reqGetNotice, reqUpdateNotice } from '@/api/system/notice'
  import { FormInstance } from 'element-plus'
  import WangEdior from '@/components/WangEdior/index.vue'

  const { proxy }: any = getCurrentInstance()
  const title = ref('添加公告')
  const dialogVisible = ref(false)
  const ruleFormRef = ref<FormInstance>()
  const form = reactive({
    noticeId: undefined,
    noticeTitle: undefined,
    noticeType: undefined,
    status: '0',
    noticeContent: undefined,
  })
  let $emit = defineEmits(['getList'])
  const rules = ref({
    noticeTitle: [{ required: true, message: '公告标题不能为空', trigger: 'blur' }],
    noticeType: [{ required: true, message: '公告类型不能为空', trigger: 'change' }],
  })

  // 提交表单
  const submitForm = async () => {
    await ruleFormRef.value.validate(async (valid, fields) => {
      if (valid) {
        if (!form.noticeId) {
          await reqAddNotice(form).then((req: any) => {
            if (req.code === 200) {
              proxy.$modal.msgSuccess('添加成功！')
              dialogVisible.value = false
              $emit('getList')
            } else {
              proxy.$modal.msgError(req.message != null ? req.message : '添加失败！')
              return
            }
          })
        } else {
          await reqUpdateNotice(form).then((req: any) => {
            if (req.code === 200) {
              proxy.$modal.msgSuccess('修改成功！')
              dialogVisible.value = false
              $emit('getList')
            } else {
              proxy.$modal.msgError(req.message != null ? req.message : '修改失败！')
              return
            }
          })
        }
      } else {
        console.log('error submit!', fields)
      }
    })
  }
  const show = async (item: any = {}) => {
    title.value = '添加公告'
    if (item.noticeId) {
      // 修改公告
      title.value = '修改公告'
      // 获取详细信息根据noticeId
      await reqGetNotice(item.noticeId)
        .then((req: any) => {
          console.log(req)
          if (req.code === 200) {
            // 进行回显
            Object.keys(form).forEach((key) => {
              form[key] = req.data[key]
            })
          } else {
            proxy.$modal.msgError('获取失败！')
            return
          }
        })
        .catch((e) => {})
    }
    dialogVisible.value = true
  }
  const wangEditRef = ref()
  // 关闭之前的回调
  const close = () => {
    // 将表单数据进行重置
    ruleFormRef.value?.resetFields()
    Object.keys(form).forEach((key) => {
      if (key === 'status') form[key] = '0'
      else form[key] = undefined
    })
  }

  defineExpose({
    show,
  })
</script>

<template>
  <el-dialog v-model="dialogVisible" @close="close" width="1080" append-to-body>
    <template #title>
      <span class="el-dialog__title">{{ title }}</span>
    </template>
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="80">
      <el-row>
        <el-col :span="24">
          <el-form-item label="公告标题" prop="noticeTitle">
            <el-input v-model="form.noticeTitle" placeholder="请输入公告标题" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="公告类型" prop="noticeType">
            <el-select v-model="form.noticeType" placeholder="请选择公告类型">
              <el-option
                v-for="dict in dict.type.sys_notice_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in dict.type.sys_notice_status"
                :key="dict.value"
                :label="dict.value"
                >{{ dict.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="内容">
            <wang-edior v-if="dialogVisible" ref="wangEditRef" v-model="form.noticeContent" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="success" @click="submitForm">提 交</el-button>
      <el-button type="info" @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
  export default {
    dicts: ['sys_notice_status', 'sys_notice_type'],
  }
</script>

<style scoped lang="scss">
  @import '../index';
</style>
